package com.yanqu.road.dao.impl.activity.suitang.data;

import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.suitang.data.CrossSuiTangUnionData;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.entity.activity.suitang.entity.city.CrossSuiTangCollect;
import com.yanqu.road.entity.activity.suitang.entity.union.CrossSuiTangCommanderData;

import java.lang.reflect.Type;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;

public class CrossSuiTangUnionDataDaoImpl extends TempDataObjectDao<CrossSuiTangUnionData> {
    public static String getCrossSuiTangUnionDataSql = "select * from `t_c_suitang_union_data` where `activityId`=? and `groupId`=?";
    public Map<String, CrossSuiTangUnionData> getCrossSuiTangUnionData(int activityId,int groupId) {
        Map<String, CrossSuiTangUnionData> unionDataMap = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getCrossSuiTangUnionDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, groupId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossSuiTangUnionData unionData = parse(rs);
                unionDataMap.put(unionData.getUnionUid(), unionData);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return unionDataMap;
    }

    public static String updateCrossSuiTangUnionDataSql = "update `t_c_suitang_union_data` set totalScore=?,redPacketValue=?,commanders=?,unionRoundMap=?,unionBuffMap=?,collectionMap=? where activityId=? and groupId=? and unionUid=?";
    @Override
    protected boolean updateData(CrossSuiTangUnionData crossSuiTangUnionData) {
        crossSuiTangUnionData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossSuiTangUnionData.getTotalScore()));
        para.put(i++, new DbParameter(Types.BIGINT, crossSuiTangUnionData.getRedPacketValue()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossSuiTangUnionData.getCommanders())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossSuiTangUnionData.getUnionRoundMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossSuiTangUnionData.getUnionBuffMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossSuiTangUnionData.getCollectionMap())));
        para.put(i++, new DbParameter(Types.INTEGER, crossSuiTangUnionData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossSuiTangUnionData.getGroupId()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossSuiTangUnionData.getUnionUid()));
        if (getDb().executeNoneQuery(updateCrossSuiTangUnionDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCrossSuiTangUnionDataSql = "insert into `t_c_suitang_union_data`(activityId,groupId,unionUid,totalScore,redPacketValue,commanders,unionRoundMap,unionBuffMap,collectionMap) values (?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(CrossSuiTangUnionData crossSuiTangUnionData) {
        crossSuiTangUnionData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossSuiTangUnionData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossSuiTangUnionData.getGroupId()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossSuiTangUnionData.getUnionUid()));
        para.put(i++, new DbParameter(Types.INTEGER, crossSuiTangUnionData.getTotalScore()));
        para.put(i++, new DbParameter(Types.BIGINT, crossSuiTangUnionData.getRedPacketValue()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossSuiTangUnionData.getCommanders())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossSuiTangUnionData.getUnionRoundMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossSuiTangUnionData.getUnionBuffMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossSuiTangUnionData.getCollectionMap())));
        if (getDb().executeNoneQuery(insertCrossSuiTangUnionDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CrossSuiTangUnionData parseData(ResultSet rs) throws SQLException {
        CrossSuiTangUnionData crossSuiTangUnionData = new CrossSuiTangUnionData();
        crossSuiTangUnionData.setActivityId(rs.getInt("activityId"));
        crossSuiTangUnionData.setGroupId(rs.getInt("groupId"));
        crossSuiTangUnionData.setUnionUid(rs.getString("unionUid"));
        crossSuiTangUnionData.setTotalScore(rs.getInt("totalScore"));
        crossSuiTangUnionData.setRedPacketValue(rs.getLong("redPacketValue"));
        crossSuiTangUnionData.setCommanders(JSON.parseObject(rs.getString("commanders"), CrossSuiTangCommanderData.class));
        crossSuiTangUnionData.setUnionRoundMap(JSON.parseObject(rs.getString("unionRoundMap"), new TypeReference<ConcurrentHashMap<java.lang.Integer, com.yanqu.road.entity.activity.suitang.entity.union.CrossSuiTangUnionRound>>() {}));
        crossSuiTangUnionData.setUnionBuffMap(JSON.parseObject(rs.getString("unionBuffMap"), new TypeReference<java.util.List<com.yanqu.road.entity.activity.suitang.entity.union.CrossSuiTangUnionBuff>>() {}));
        crossSuiTangUnionData.setCollectionMap(JSON.parseObject(rs.getString("collectionMap"), new TypeReference<ConcurrentHashMap<java.lang.Long, java.util.Map<java.lang.Integer, CrossSuiTangCollect>>>() {}));

        return crossSuiTangUnionData;
    }
}
